package com.tju.ocean_ecology_website.controller;

import com.tju.ocean_ecology_website.dto.ApiResponse;
import com.tju.ocean_ecology_website.dto.PageResult;
import com.tju.ocean_ecology_website.entity.Activity;
import com.tju.ocean_ecology_website.entity.ActivityFeedback;
import com.tju.ocean_ecology_website.entity.ActivitySignup;
import com.tju.ocean_ecology_website.entity.User;
import com.tju.ocean_ecology_website.entity.VolunteerInfo;
import com.tju.ocean_ecology_website.service.ActivityFeedbackService;
import com.tju.ocean_ecology_website.service.ActivityService;
import com.tju.ocean_ecology_website.service.ActivitySignupService;
import com.tju.ocean_ecology_website.service.UserService;
import com.tju.ocean_ecology_website.service.VolunteerInfoService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 志愿者活动控制器
 */
@RestController
@RequestMapping("/api/volunteer")
public class VolunteerActivitiesController {

    @Resource
    private UserService userService;

    @Resource
    private VolunteerInfoService volunteerInfoService;

    @Resource
    private ActivityService activityService;

    @Resource
    private ActivitySignupService activitySignupService;

    @Resource
    private ActivityFeedbackService activityFeedbackService;

    /**
     * 获取志愿者活动列表
     *
     * @param page 页码
     * @param pageSize 每页大小
     * @param status 状态：0-即将开始, 1-进行中, 2-已结束
     * @return 活动列表
     */
    @GetMapping("/activities")
    public ResponseEntity<ApiResponse<PageResult<Activity>>> getVolunteerActivities(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "status", required = false) Integer status) {

        // 查询活动列表
        Activity activityQuery = new Activity();
        if (status != null) {
            activityQuery.setStatus(status);
        }
        activityQuery.setAuditStatus(1); // 已通过
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize);
        Page<Activity> activityPage = activityService.queryByPage(activityQuery, pageRequest);

        // 创建分页结果
        PageResult<Activity> pageResult = PageResult.fromPage(activityPage, page);

        return ResponseEntity.ok(ApiResponse.success(pageResult));
    }

    /**
     * 获取活动详情
     *
     * @param id 活动ID
     * @return 活动详情
     */
//    @GetMapping("/activities/{id}")
    public ResponseEntity<ApiResponse<Activity>> getActivityDetail(@PathVariable("id") Long id) {

        // 查询活动信息
        Activity activity = activityService.queryById(id);
        if (activity == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "活动不存在"));
        }

        return ResponseEntity.ok(ApiResponse.success(activity));
    }

    /**
     * 参与活动
     *
     * @param id 活动ID
     * @return 参与结果
     */
    @PostMapping("/activities/{id}/join")
    public ResponseEntity<ApiResponse<ActivitySignup>> joinActivity(@PathVariable("id") Long id) {

        // 查询活动信息
        Activity activity = activityService.queryById(id);
        if (activity == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "活动不存在"));
        }

        // 检查活动状态
        if (activity.getStatus() != 0) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "活动已开始或已结束，无法参与"));
        }

        // 创建报名记录
        ActivitySignup signup = new ActivitySignup();
        signup.setActivityId(id);
        signup.setStatus(0); // 待审核
        signup.setSignupTime(new Date());

        // 保存报名记录
        ActivitySignup savedSignup = activitySignupService.insert(signup);

        return ResponseEntity.ok(ApiResponse.success(savedSignup, "参与活动成功，等待审核"));
    }

    // 移除活动签到方法

    /**
     * 提交活动反馈
     *
     * @param id 活动ID
     * @param feedback 反馈信息
     * @return 提交结果
     */
    @PostMapping("/activities/{id}/feedback")
    public ResponseEntity<ApiResponse<ActivityFeedback>> submitActivityFeedback(
            @PathVariable("id") Long id,
            @RequestBody ActivityFeedback feedback) {

        // 查询活动信息
        Activity activity = activityService.queryById(id);
        if (activity == null) {
            return ResponseEntity.badRequest().body(ApiResponse.error(400, "活动不存在"));
        }

        // 设置反馈信息
        feedback.setActivityId(id);
        feedback.setCreateTime(new Date());

        // 保存反馈信息
        ActivityFeedback savedFeedback = activityFeedbackService.insert(feedback);

        return ResponseEntity.ok(ApiResponse.success(savedFeedback, "提交反馈成功"));
    }


}
